home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / AmigaSystem / AmiStart / ToolsSDK.lha / source / StartUp.c < prev    next >
C/C++ Source or Header  |  2002-06-01  |  11KB  |  316 lines

  1. /*
  2. **      $VER: StartUp.c 37.31 (18.3.98)
  3. **
  4. **      Library startup-code and function table definition
  5. **
  6. **      (C) Copyright 1996-98 Andreas R. Kleinert
  7. **      All Rights Reserved.
  8. **
  9. **        modified by adding functions by Darius Brewka
  10. */
  11.  
  12. #define __USE_SYSBASE        // perhaps only recognized by SAS/C
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <exec/libraries.h>
  17. #include <exec/execbase.h>
  18. #include <exec/resident.h>
  19. #include <exec/initializers.h>
  20.  
  21.  
  22. #ifdef __MAXON__
  23. #include <pragma/exec_lib.h>
  24. #include <linkerfunc.h>
  25. #else
  26. #include <proto/exec.h>    // all other compilers
  27. #endif
  28. #include "compiler.h"
  29.  
  30. #include "amistart_modulebase.h"
  31.  
  32. #include "ModuleFuncs.h"
  33.  
  34.  
  35. extern ULONG __saveds __stdargs L_OpenLibs(struct AmiStartModuleBase *AmiStartModuleBase);
  36. extern void  __saveds __stdargs L_CloseLibs(void);
  37.  
  38. struct AmiStartModuleBase * __saveds ASM InitLib( register __a6 struct ExecBase    *sysbase GNUCREG(a6),
  39.                                            register __a0 SEGLISTPTR          seglist GNUCREG(a0),
  40.                                            register __d0 struct AmiStartModuleBase *exb     GNUCREG(d0));
  41. struct AmiStartModuleBase * __saveds ASM OpenLib( register __a6 struct AmiStartModuleBase *AmiStartModuleBase GNUCREG(a6));
  42. SEGLISTPTR __saveds ASM CloseLib( register __a6 struct AmiStartModuleBase *AmiStartModuleBase GNUCREG(a6));
  43. SEGLISTPTR __saveds ASM ExpungeLib( register __a6 struct AmiStartModuleBase *exb GNUCREG(a6));
  44. ULONG ASM ExtFuncLib(void);
  45.  
  46.  
  47. /* ----------------------------------------------------------------------------------------
  48.    ! LibStart:
  49.    !
  50.    ! If someone tries to start a library as an executable, it must return (LONG) -1
  51.    ! as result. That's what we are doing here.
  52.    ---------------------------------------------------------------------------------------- */
  53.  
  54. LONG ASM LibStart(void)
  55. {
  56.  return(-1);
  57. }
  58.  
  59.  
  60. /* ----------------------------------------------------------------------------------------
  61.    ! Function and Data Tables:
  62.    !
  63.    ! The function and data tables have been placed here for traditional reasons.
  64.    ! Placing the RomTag structure before (-> LibInit.c) would also be a good idea,
  65.    ! but it depends on whether you would like to keep the "version" stuff separately.
  66.    ---------------------------------------------------------------------------------------- */
  67.  
  68. extern APTR FuncTab [];
  69. /*  extern struct MyDataInit DataTab;  */
  70. extern DataTab; /* DICE fix */
  71.                                   /* Instead you may place ROMTag + Datatab directly, here */
  72.                                   /* (see LibInit.c). This may fix "Installer" version     */
  73.                                   /* checking problems, too - try it.                      */
  74.  
  75. struct InitTable                       /* do not change */
  76. {
  77.  ULONG              LibBaseSize;
  78.  APTR              *FunctionTable;
  79.  struct MyDataInit *DataTable;
  80.  APTR               InitLibTable;
  81. } InitTab =
  82. {
  83.  (ULONG)               sizeof(struct AmiStartModuleBase),
  84.  (APTR              *) &FuncTab[0],
  85.  (struct MyDataInit *) &DataTab,
  86.  (APTR)                InitLib
  87. };
  88.  
  89. APTR FuncTab [] =
  90. {
  91.  (APTR) OpenLib,
  92.  (APTR) CloseLib,
  93.  (APTR) ExpungeLib,
  94.  (APTR) ExtFuncLib,
  95.  
  96.  (APTR) OpenTool,  /* add your own functions here */
  97.  (APTR) CloseTool,
  98.  (APTR) ToolInfo,
  99.  (APTR) NewTool,
  100.  (APTR) ToolWidth,
  101.  (APTR) ToolHeight,
  102.  (APTR) InitTool,
  103.  (APTR) DisposeTool,
  104.  (APTR) GetToolFlags,
  105.  (APTR) GetToolPort,
  106.  (APTR) ToolSetup,
  107.  (APTR) GetToolPrefs,
  108.  (APTR) GetToolPrefsSize,
  109.  (APTR) GetToolBufferType,
  110.  
  111.  (APTR) ((LONG)-1),
  112. };
  113.  
  114.  
  115. extern struct AmiStartModuleBase *AmiStartModuleBase;
  116.  
  117. /* ----------------------------------------------------------------------------------------
  118.    ! InitLib:
  119.    !
  120.    ! This one is single-threaded by the Ramlib process. Theoretically you can do, what
  121.    ! you like here, since you have full exclusive control over all the library code and data.
  122.    ! But due to some bugs in Ramlib V37-40, you can easily cause a deadlock when opening
  123.    ! certain libraries here (which open other libraries, that open other libraries, that...)
  124.    !
  125.    ---------------------------------------------------------------------------------------- */
  126.  
  127. struct AmiStartModuleBase * __saveds ASM InitLib( register __a6 struct ExecBase      *sysbase GNUCREG(a6),
  128.                                            register __a0 SEGLISTPTR            seglist GNUCREG(a0),
  129.                                            register __d0 struct AmiStartModuleBase   *exb     GNUCREG(d0))
  130. {
  131.  AmiStartModuleBase = exb;
  132.  
  133.  AmiStartModuleBase->exb_SysBase = sysbase;
  134.  AmiStartModuleBase->exb_SegList = seglist;
  135.  
  136.  if(L_OpenLibs(AmiStartModuleBase)) return(AmiStartModuleBase);
  137.  
  138.  L_CloseLibs();
  139.  
  140.   {
  141.    ULONG negsize, possize, fullsize;
  142.    UBYTE *negptr = (UBYTE *) AmiStartModuleBase;
  143.  
  144.    negsize  = AmiStartModuleBase->exb_LibNode.lib_NegSize;
  145.    possize  = AmiStartModuleBase->exb_LibNode.lib_PosSize;
  146.    fullsize = negsize + possize;
  147.    negptr  -= negsize;
  148.  
  149.    FreeMem(negptr, fullsize);
  150.  
  151.    #ifdef __MAXON__
  152.    CleanupModules();
  153.    #endif
  154.   }
  155.  
  156.  return(NULL);
  157. }
  158.  
  159. /* ----------------------------------------------------------------------------------------
  160.    ! OpenLib:
  161.    !
  162.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  163.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  164.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  165.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  166.    ! function.
  167.    !
  168.    ! Currently you only can bypass this restriction by supplying your own semaphore
  169.    ! mechanism.
  170.    ---------------------------------------------------------------------------------------- */
  171.  
  172. struct AmiStartModuleBase * __saveds ASM OpenLib( register __a6 struct AmiStartModuleBase *AmiStartModuleBase GNUCREG(a6))
  173. {
  174.  #ifdef __MAXON__
  175.  GetBaseReg();
  176.  InitModules();
  177.  #endif
  178.  
  179.  AmiStartModuleBase->exb_LibNode.lib_OpenCnt++;
  180.  
  181.  AmiStartModuleBase->exb_LibNode.lib_Flags &= ~LIBF_DELEXP;
  182.  
  183.  return(AmiStartModuleBase);
  184. }
  185.  
  186. /* ----------------------------------------------------------------------------------------
  187.    ! CloseLib:
  188.    !
  189.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  190.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  191.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  192.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  193.    ! function.
  194.    !
  195.    ! Currently you only can bypass this restriction by supplying your own semaphore
  196.    ! mechanism.
  197.    ---------------------------------------------------------------------------------------- */
  198.  
  199. SEGLISTPTR __saveds ASM CloseLib( register __a6 struct AmiStartModuleBase *AmiStartModuleBase GNUCREG(a6))
  200. {
  201.  AmiStartModuleBase->exb_LibNode.lib_OpenCnt--;
  202.  
  203.  if(!AmiStartModuleBase->exb_LibNode.lib_OpenCnt)
  204.   {
  205.    if(AmiStartModuleBase->exb_LibNode.lib_Flags & LIBF_DELEXP)
  206.     {
  207.      return( ExpungeLib(AmiStartModuleBase) );
  208.     }
  209.   }
  210.  
  211.  return(NULL);
  212. }
  213.  
  214. /* ----------------------------------------------------------------------------------------
  215.    ! ExpungeLib:
  216.    !
  217.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  218.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  219.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  220.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  221.    ! function.
  222.    !
  223.    ! Currently you only could bypass this restriction by supplying your own semaphore
  224.    ! mechanism - but since expunging can't be done twice, one should avoid it here.
  225.    ---------------------------------------------------------------------------------------- */
  226.  
  227. SEGLISTPTR __saveds ASM ExpungeLib( register __a6 struct AmiStartModuleBase *exb GNUCREG(a6))
  228. {
  229.  struct AmiStartModuleBase *AmiStartModuleBase = exb;
  230.  SEGLISTPTR seglist;
  231.  
  232.  if(!AmiStartModuleBase->exb_LibNode.lib_OpenCnt)
  233.   {
  234.    ULONG negsize, possize, fullsize;
  235.    UBYTE *negptr = (UBYTE *) AmiStartModuleBase;
  236.  
  237.    seglist = AmiStartModuleBase->exb_SegList;
  238.  
  239.    Remove((struct Node *)AmiStartModuleBase);
  240.  
  241.    L_CloseLibs();
  242.  
  243.    negsize  = AmiStartModuleBase->exb_LibNode.lib_NegSize;
  244.    possize  = AmiStartModuleBase->exb_LibNode.lib_PosSize;
  245.    fullsize = negsize + possize;
  246.    negptr  -= negsize;
  247.  
  248.    FreeMem(negptr, fullsize);
  249.  
  250.    #ifdef __MAXON__
  251.    CleanupModules();
  252.    #endif
  253.  
  254.    return(seglist);
  255.   }
  256.  
  257.  AmiStartModuleBase->exb_LibNode.lib_Flags |= LIBF_DELEXP;
  258.  
  259.  return(NULL);
  260. }
  261.  
  262. /* ----------------------------------------------------------------------------------------
  263.    ! ExtFunct:
  264.    !
  265.    ! This one is enclosed within a Forbid/Permit pair by Exec V37-40. Since a Wait() call
  266.    ! would break this Forbid/Permit(), you are not allowed to start any operations that
  267.    ! may cause a Wait() during their processing. It's possible, that future OS versions
  268.    ! won't turn the multi-tasking off, but instead use semaphore protection for this
  269.    ! function.
  270.    !
  271.    ! Currently you only can bypass this restriction by supplying your own semaphore
  272.    ! mechanism - but since this function currently is unused, you should not touch
  273.    ! it, either.
  274.    ---------------------------------------------------------------------------------------- */
  275.  
  276. ULONG ASM ExtFuncLib(void)
  277. {
  278.  return(NULL);
  279. }
  280.  
  281. struct AmiStartModuleBase *AmiStartModuleBase = NULL;
  282.  
  283.  
  284. /* ----------------------------------------------------------------------------------------
  285.    ! __SASC stuff:
  286.    !
  287.    ! This is only for SAS/C - its intention is to turn off internal CTRL-C handling
  288.    ! for standard C functions and to avoid calls to exit() et al.
  289.    ---------------------------------------------------------------------------------------- */
  290.  
  291. #ifdef __SASC
  292.  
  293. #ifdef ARK_OLD_STDIO_FIX
  294.  
  295. ULONG XCEXIT       = NULL; /* These symbols may be referenced by    */
  296. ULONG _XCEXIT      = NULL; /* some functions of sc.lib, but should  */
  297. ULONG ONBREAK      = NULL; /* never be used inside a shared library */
  298. ULONG _ONBREAK     = NULL;
  299. ULONG base         = NULL; /* Note, that XCEXIT/ONBREAK actually    */
  300. ULONG _base        = NULL; /* should have been defined as functions */
  301. ULONG ProgramName  = NULL; /* and not as ULONGs...                  */
  302. ULONG _ProgramName = NULL;
  303. ULONG StackPtr     = NULL;
  304. ULONG _StackPtr    = NULL;
  305. ULONG oserr        = NULL;
  306. ULONG _oserr       = NULL;
  307. ULONG OSERR        = NULL;
  308. ULONG _OSERR       = NULL;
  309.  
  310. #endif /* ARK_OLD_STDIO_FIX */
  311.  
  312. void __regargs __chkabort(void) { }  /* a shared library cannot be    */
  313. void __regargs _CXBRK(void)     { }  /* CTRL-C aborted when doing I/O */
  314.  
  315. #endif /* __SASC */
  316.